package com.smartTerminal.perMatters.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.smartTerminal.perMatters.entity.Approval;
import com.smartTerminal.perMatters.entity.Bussiness;
import com.smartTerminal.perMatters.service.MyBussinessService;
import com.smartTerminal.user.entity.User;
import com.smartTerminal.utils.StringUtils;

@Controller
@RequestMapping("/MyBussinessController")
public class MyBussinessController {
	
	@Autowired
	private MyBussinessService bussinessService;

	public MyBussinessService getBussinessService() {
		return bussinessService;
	}

	public void setBussinessService(MyBussinessService bussinessService) {
		this.bussinessService = bussinessService;
	}
	
	/**
	 * 跳转到办事查询选择硬件页面
	 * @method	queryBussiness
	 * @author	Xia.Zhuming
	 * @data	2018年1月14日
	 * @param request
	 * @param bussiness
	 * @return
	 */
	@RequestMapping(value="/queryBussiness",method={RequestMethod.GET,RequestMethod.POST})
	public String queryBussiness(HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("USER");
		if(user == null || StringUtils.isEmpty(user.getAuthToken())){
			user = new User();
			user.setService("permatters/scheduleSearch");
			request.setAttribute("user", user);
			return "user/login";
		}
		return "permatters/scheduleSearch";
	}
	
	/**
	 * 办事进度查询
	 * @method	getBusinessInfo
	 * @author	Xia.Zhuming
	 * @data	2018年1月16日
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/getBusinessInfo",method={RequestMethod.GET,RequestMethod.POST})
	public String getBusinessInfo(HttpServletRequest request){
		String order = request.getParameter("data");
		List<Approval> list = null;
		try {
			list = bussinessService.getBusinessInfo(order);
		} catch (Exception e) {
			request.setAttribute("tips", e.getMessage());
			return "user/error";
		}
		for (Approval approval : list) {
			request.setAttribute("approval", approval);
		}
		return "permatters/scheduleApproval";
	}

	/**
	 * 获取办件列表
	 * @method	queryBussinessList
	 * @author	Xia.Zhuming
	 * @data	2018年1月10日
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/queryBussinessList",method={RequestMethod.GET,RequestMethod.POST})
	public String queryBussinessList(HttpServletRequest request, Bussiness bussiness){
		User user = (User) request.getSession().getAttribute("USER");
		List<Bussiness> list = new ArrayList<Bussiness>();
		try {
			list = bussinessService.queryBussinessList(bussiness, user.getAuthToken());
		} catch (Exception e) {
			request.setAttribute("tips", e.getMessage());
			return "user/error";
		}
		request.setAttribute("BussinessList", list);
		request.setAttribute("bussiness", bussiness);
		return "permatters/scheduleTable";
	}
	
	/**
	 * 获取办件详情
	 * @method	getBusinessById
	 * @author	Xia.Zhuming
	 * @data	2018年1月10日
	 * @param bussiness
	 * @return
	 */
	@RequestMapping(value="/getBusinessById",method={RequestMethod.GET,RequestMethod.POST})
	public String getBusinessById(HttpServletRequest request, Bussiness bussiness){
		try {
			bussiness = bussinessService.getBusinessById(bussiness);
		} catch (Exception e) {
			request.setAttribute("tips", e.getMessage());
			return "user/error";
		}
		request.setAttribute("BussinessInfo", bussiness);
		return "permatters/scheduleInfo";
	}
	
	/**
	 * 事项查询
	 * @method	getItem
	 * @author	Xia.Zhuming
	 * @data	2018年1月13日
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/getItem",method={RequestMethod.GET,RequestMethod.POST})
	public String getItem(HttpServletRequest request, HttpServletResponse response){
		request.setAttribute("type", "2");//个人办事
		return "permatters/itemSearch";
	}
}
